package vip.xiaonuo.core.factory;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.mail.MailAccount;
import vip.xiaonuo.core.context.constant.ConstantContextHolder;
import vip.xiaonuo.core.email.MailSender;
import vip.xiaonuo.core.email.modular.SimpleMailSender;
import vip.xiaonuo.core.email.modular.model.SendMailParam;
import vip.xiaonuo.core.pojo.email.EmailConfigs;

import java.util.Objects;

/**
 * 邮件发送
 * @author yaorw
 */
public enum MailSenderFactory {

    /**
     * 实例
     */
    INSTANCE;

    private MailSender sender;

    public void clear() {
        this.sender = null;
    }

    public void send(SendMailParam sendMailPo) {
        getOrInitMailSender().sendMail(sendMailPo);
    }

    public void sendHtml(SendMailParam sendMailPo) {
        getOrInitMailSender().sendMailHtml(sendMailPo);
    }

    private MailSender getOrInitMailSender() {
        if (Objects.nonNull(sender)) {
            return sender;
        }
        EmailConfigs emailConfigs = ConstantContextHolder.getEmailConfigs();
        MailAccount mailAccount = new MailAccount();
        BeanUtil.copyProperties(emailConfigs, mailAccount);
        sender = new SimpleMailSender(mailAccount);
        return sender;
    }

}
